<template>
  <div class="file-upload">
    <h3>文件上传分享</h3>
    <div class="upload-area" @dragover.prevent @drop="handleDrop">
      <input 
        type="file" 
        @change="handleFileChange" 
        class="file-input"
        :disabled="isUploading"
      >
      <div v-if="!selectedFile && !isUploading" class="upload-hint">
        点击或拖拽文件到此处上传
      </div>
      <div v-if="selectedFile && !isUploading" class="file-info">
        <p>文件名: {{ selectedFile.name }}</p>
        <p>大小: {{ formatFileSize(selectedFile.size) }}</p>
      </div>
      <div v-if="isUploading" class="uploading">
        <p>上传中...</p>
        <div class="progress-bar">
          <div class="progress" :style="{ width: progress + '%' }"></div>
        </div>
      </div>
    </div>
    <button 
      @click="uploadFile" 
      class="upload-btn"
      :disabled="!selectedFile || isUploading"
    >
      {{ isUploading ? '上传中...' : '上传分享' }}
    </button>
    <div v-if="shareLink" class="share-link">
      <p>分享链接: <a :href="shareLink" target="_blank">{{ shareLink }}</a></p>
      <button @click="copyLink" class="copy-btn">复制链接</button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useFileUpload } from '../hooks/useUpload.ts'

// 使用文件上传钩子
const {
  selectedFile,
  shareLink,
  isUploading,
  progress,
  handleFileChange,
  handleDrop,
  formatFileSize,
  uploadFile,
  copyLink
} = useFileUpload()
</script>

<style scoped>
/* 样式保持不变，新增上传进度条样式 */
.file-upload {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ddd;
  border-radius: 8px;
  margin-top: 30px;
}

.upload-area {
  width: 100%;
  height: 200px;
  border: 2px dashed #ddd;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-bottom: 15px;
  position: relative;
}

.file-input {
  position: absolute;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

.upload-hint {
  color: #999;
  text-align: center;
  padding: 20px;
}

.file-info {
  text-align: center;
}

.uploading {
  text-align: center;
}

.progress-bar {
  width: 80%;
  height: 10px;
  background-color: #f0f0f0;
  border-radius: 5px;
  margin: 10px auto;
  overflow: hidden;
}

.progress {
  height: 100%;
  background-color: #42b983;
  transition: width 0.3s;
}

.upload-btn {
  padding: 8px 16px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.upload-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.share-link {
  margin-top: 20px;
  padding: 10px;
  background-color: #f5f5f5;
  border-radius: 4px;
}

.copy-btn {
  margin-left: 10px;
  padding: 4px 8px;
  background-color: #2196f3;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
</style>